package com.touzbi.hss.pagemanager;

import com.touzbi.hss.parametermanager.AbstractParameterManager;

public class HttpPageManager implements AbstractPageManager {
	private final String PAGE_NUMBER_PARAM = "page";

	private AbstractParameterManager paramManager;
	private int pageSize;

	public HttpPageManager(int pageSize, AbstractParameterManager paramManager) {
		super();
		this.pageSize = pageSize;
		this.paramManager = paramManager;
	}

	public int getPageNumber() {
		String pageNumberParam = this.paramManager
				.getParamValue(PAGE_NUMBER_PARAM);
		int pageNumber;
		if (pageNumberParam == null) {
			pageNumber = 0;
		} else {
			pageNumber = Integer.valueOf(pageNumberParam);
			if (pageNumber > 0) {
				pageNumber--;
			}
		}
		return pageNumber;
	}

	public int translatePageIndex(int oldPageIndex) {
		return oldPageIndex + 1;
	}

	public int getPageCount(int totalCount) {
		return (int) Math.ceil((double) totalCount / (double) this.pageSize);
	}

	@Override
	public int getPageSize() {
		return this.pageSize;
	}

}
